package com.cuanbo.dualscreenteachzhu.utils;

import android.text.TextUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * 字符串拼接工具类
 */
public class StringUtil {
    private static final String regex = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}";

    /**
     * 将strHead作为字符串的头与cmdList进行拼接，cmdList中的内容将用"|"隔开
     */
    public static String get(String strHead, List<String> cmdList) {
        if (cmdList != null) {
            for (int i = 0; i < cmdList.size(); i++) {
                if (i != cmdList.size() - 1) {
                    strHead += cmdList.get(i) + "|";
                } else {
                    strHead += cmdList.get(i);
                }
            }
        }
        return strHead;
    }

    /**
     * 将字节数组格式化为十六进制字符串
     */
    public static String fmtHex(byte[] ary, int len) {
        String str = "";
        for (int i = 0; i < len; i++) {
            if (i == len - 1) {
                str += String.format("%02X", ary[i]);
            } else {
                str += String.format("%02X", ary[i]) + " ";
            }
        }
        return str;
    }

    /**
     * RGB16进制字符串转RGB数组
     *
     * @param rgb RGB16进制字符串
     */
    public static int[] getRGB(String rgb) {
        String r = rgb.substring(0, 2);
        String g = rgb.substring(2, 4);
        String b = rgb.substring(4, 6);
        int[] rgbAry = new int[3];
        rgbAry[0] = Integer.valueOf(r, 16);
        rgbAry[1] = Integer.valueOf(g, 16);
        rgbAry[2] = Integer.valueOf(b, 16);
        return rgbAry;
    }

    /**
     * 16进制字符串转十进制整数
     */
    public static int hexToDecimal(String strHex) {
        return Integer.valueOf(strHex, 16);
    }

    /**
     * 16进制字符串转byte
     */
    public static byte hexToByte(String strHex) {
        return (byte)((int) Integer.valueOf(strHex, 16));
    }

    /**
     * 检查IP格式是否正确
     */
    public static boolean checkIpFormat(String ip) {
        if (ip == null || ip.trim().equals("")) return true;
        return Pattern.compile(regex).matcher(ip).matches();
    }

    /**
     * 检查字符串是否为空
     *
     * @param str 字符串
     */
    public static String checkEmpty(String str) {
        if (str == null) {
            return "";
        } else {
            return str;
        }
    }

    /**
     * boolean转Html格式字符串
     *
     * @param bl 布尔值
     * @return 转化为Html格式字符串
     */
    public static String htmlCheck(boolean bl) {
        if (bl) {
            return " checked=\"checked\" ";
        } else {
            return "";
        }
    }

    /**
     * html字符串转boolean
     *
     * @param htmlStr
     */
    public static boolean strToBoolean(String htmlStr) {
        if (htmlStr == null || htmlStr.equals("null")) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * html字符串转int
     *
     * @param htmlStr  html字符串
     * @param defValue 默认值
     */
    public static int strToInt(String htmlStr, int defValue) {
        if (htmlStr == null || htmlStr.trim().equals("null") || htmlStr.trim().equals("")) {
            return defValue;
        } else {
            return Integer.valueOf(htmlStr.trim());
        }
    }

    /**
     * html字符串转字符串集合
     *
     * @param str 字符串
     * @return 字符串集合
     */
    public static List<String> strToList(String str) {
        String[] ary = null;
        if (TextUtils.isEmpty(str)) {
            ary = new String[]{};
        } else {
            ary = str.split("\\|");
        }
        List<String> list = new ArrayList<>();
        for (int i = 0; i < ary.length; i++) {
            list.add(ary[i]);
        }
        return list;
    }

    /**
     * 16进制字符串转byte数组
     *
     * @param inHex 16进制字符串
     */
    public static byte[] hex2Byte(String inHex) {
        String[] hex = inHex.split(" ");//将接收的字符串按空格分割成数组
        byte[] byteArray = new byte[hex.length];

        for (int i = 0; i < hex.length; i++) {
            //parseInt()方法用于将字符串参数作为有符号的n进制整数进行解析
            byteArray[i] = (byte) Integer.parseInt(hex[i], 16);
        }
        return byteArray;
    }

    /**
     * 获取中兴值
     *
     * @param str 字符串数据
     * @return double数组
     */
    public static double[] getZXValue(String str) {
        double[] douAry = null;
        try {
            String[] strAry = str.split(",");
            douAry = new double[strAry.length];
            for (int i = 0; i < strAry.length; i++) {
                douAry[i] = Double.valueOf(strAry[i]);
            }
        } catch (Exception e) {
        }
        return douAry;
    }

    public static double getDValue(String str){
        double d=0;
        try{
            return d= Double.valueOf(str);
        }catch (Exception e){
        }
        return d;
    }
}
